package com.example.pufang.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.PageParamRequest;
import com.example.pufang.mapper.UserMapper;
import com.example.pufang.pojo.User;
import com.example.pufang.service.UserService;
import com.example.pufang.vo.PageListVo;
import com.example.utils.DESUtils;
import com.example.utils.PufangUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public PageListVo getListByPage(PageParamRequest pageParamRequest, String searchPhone, String searchName,
                                    String searchRealName) {

        PageListVo pageListVo = new PageListVo();

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(searchPhone), User::getPhone, searchPhone)
                .like(StringUtils.hasText(searchName), User::getUsername, searchName)
                .like(StringUtils.hasText(searchRealName), User::getRealName, searchRealName);

        Page<User> page = new Page<>(pageParamRequest.getPage(), pageParamRequest.getLimit());
        IPage<User> userIPage = userMapper.selectPage(page, wrapper);

        pageListVo.setPage(userIPage.getCurrent())
                .setSize(userIPage.getSize())
                .setList(JSONObject.toJSONString(userIPage.getRecords()))
                .setTotal(userIPage.getTotal())
                .setTotalPage(userIPage.getPages());

        return pageListVo;
    }

    /**
     * 通过手机号码查询用户
     * @param phone
     * @return
     */
    @Override
    public User getUserByPhone(String phone) {

        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasText(phone), User::getPhone, phone);

        return baseMapper.selectOne(wrapper);
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @Override
    public Boolean register(User user) {

        // 加密密码
       user.setPassword(DESUtils.encrypt(user.getPassword()));

        // 随机用户名，默认注册日期
        user.setUsername(new PufangUtils().getUuid().substring(0, 4))
                .setCreateTime(new Date())
                .setMoney(BigDecimal.ZERO);

        return this.save(user);
    }

    /**
     * 封禁用户
     * @param user
     * @return
     */
    @Override
    public Boolean ban(User user) {

        User databaseUser = baseMapper.selectById(user.getId());
        if (ObjectUtils.isNull(databaseUser)) {
            return false;
        } else {
            databaseUser.setIsBan(true);
        }
        return baseMapper.updateById(databaseUser) > 0 ? true : false;
    }

    /**
     * 解封用户
     * @param user
     * @return
     */
    @Override
    public Boolean cancelBan(User user) {

        User databaseUser = baseMapper.selectById(user.getId());

        if (ObjectUtils.isNull(databaseUser)) {
            return false;
        } else {
            databaseUser.setIsBan(false);
        }

        return baseMapper.updateById(databaseUser) > 0 ? true : false;
    }




}
